package cn.wyh.controller;

import cn.wyh.entity.Users;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PreFilter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wyh
 * @version 1.0
 * @date 2021/10/3 14:06
 */
@RestController
@RequestMapping("test")
public class TestController {

    @GetMapping("hello")
    public String hello(){
        return "spring security hello";
    }

    @GetMapping("index")
    public String index(){
        return "spring security index";
    }

    @Secured(value = {"ROLE_sale","ROLE_admin"})
    @GetMapping("update")
    public String update(){
        return "spring security update";
    }

    @PreAuthorize("hasAnyAuthority('admin')")
    @GetMapping("add")
    public String add(){
        return "spring security add";
    }

    @PostAuthorize("hasAnyAuthority('del')")
    @GetMapping("del")
    public String del(){
        System.out.println("del执行了-----");
        return "spring security del";
    }


    @GetMapping("getAll")
    @PostFilter("filterObject.username=='admin'")
    public List<Users> getAll(){
        List<Users> list = new ArrayList<>();
        list.add(new Users("1","admin","123"));
        list.add(new Users("2","q","1"));
        return list;
    }

    @GetMapping("getTestPreFilter")
    @PreFilter("filterObject.id == '2'")
    public List<Users> getTestPreFilter(){
        List<Users> list = new ArrayList<>();
        list.add(new Users("1","admin","123"));
        list.add(new Users("2","q","1"));
        return list;
    }
}
